<?php
/*	Created by Alexander Alexandrov aka SS
			hazg@mail.ru				*/	

define('SSLIB_Session', TRUE);
//if(!defined("SSLIB_Database")) die(__FILE__." Database class missing");
require('text.class.php');
require('user.class.php');
session_start();

/**
 * Sessions works layer. In tests....
 *
 */
class Session
{
	private static $COOKIENAME 		= 'sslibsession';
	private static $COOK_ID 		= 0;
	private static $COOK_SECUR 		= 1;
	
	static function Begin()
	{
		if(array_key_exists(Session::$COOKIENAME, $_SESSION)){return true;}
		if($cookies = Session::getCookie())
		{
			$_SESSION[Session::$COOKIENAME] = $cookies[Session::$COOK_ID];
		}
		return false;
	}
	/**
	 * Return current user information
	 *
	 * @return User
	 */
	static function User()
	{
		//if($_SESSION['sslibuserinfo'] )
		return new User(Session::UserID());
	}
	
	static function UserID($login = false)
	{
		if(!$login)
		{
			if($cookies = Session::getCookie())
				return $cookies[Session::$COOK_ID];
		}
		else 
		{
			
			$login = Sql::Quote(Text::Lower(Sql::Escape($login)));
			$db = Sslib::DB();
			$data = $db->Query('SELECT id FROM '.Sslib::TableUsers()." WHERE LOWER(nick)=$login LIMIT 1");
			if($data->IsEmpty()) return false;
			$row = $data->Row();
			return $row['id'];
		}
		return false;
	}
	static function Logout()
	{
		Session::clearClientCookies();
		session_unregister(Session::$COOKIENAME);
	}
	static function Check($login, $password)
	{
		$id = Session::UserID($login);
		if(!$id) return false;
		return Session::checkEncrypted($id, Session::encrypt($password));
	}
	
	static function Login($login, $password)
	{
		Session::clearClientCookies();
		Session::storeSession(Session::UserID($login),  Session::encrypt($password));
	}
	
	
	private static function storeSession($id, $encpass)
	{
		$arr = array();
		$arr[Session::$COOK_ID] = $id;
		$arr[Session::$COOK_SECUR] = $encpass;
		$_SESSION[Session::$COOKIENAME] = $id;
		Session::setCookie(Session::$COOKIENAME, implode('/',$arr));
	}
	private static function setCookie($name, $val){setcookie($name, $val, time()+60*60*24*30, "/", false, 0);}
	private static function getCookie()
	{
		
		if(array_key_exists(Session::$COOKIENAME, $_COOKIE))
		{
			$arr = explode('/',$_COOKIE[Session::$COOKIENAME]);
			if(sizeof($arr) < 2){ clearCLientCookies(); return false;}
			if($arr[Session::$COOK_ID] < 1) return false;
			if(empty($arr[Session::$COOK_SECUR])) return false;
			return $arr;
		}
		return false;
	}
	private static function encrypt($text)	{return md5($text);							}
	private static function clearClientCookies()	{setcookie(Session::$COOKIENAME, '', -1);	}
	private static function checkEncrypted($id, $password)
	{
		$db = Sslib::DB();
		$data = $db->Query('SELECT pass FROM '.Sslib::TableUsers()." WHERE id='$id' LIMIT 1");
		if($data->IsEmpty()) return false;
		$row = $data->Row();
		if( Session::encrypt($row['pass']) == $password){return true;}
		
		return false;
	}
	
}
?>